package com.bl.weilan.service.biz.companyteam.impl;

import com.bl.weilan.common.utils.UUIDUtils;
import com.bl.weilan.dao.model.CompanyTeamEntity;
import com.bl.weilan.dao.model.PeopleEntity;
import com.bl.weilan.dao.repository.CompanyTeamRepository;
import com.bl.weilan.dao.repository.PeopleRepository;
import com.bl.weilan.service.biz.companyteam.CompanyTeamService;
import com.bl.weilan.service.vo.DelBody;
import com.bl.weilan.service.vo.companyteam.CompanyTeamAddVo;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@Log4j2
public class CompanyTeamServiceImpl implements CompanyTeamService {

    @Autowired
    CompanyTeamRepository companyTeamRepository;
    @Autowired
    PeopleRepository peopleRepository;


    @Override
    public void add(CompanyTeamAddVo companyTeamAddVo) {
        CompanyTeamEntity companyTeamEntity = new CompanyTeamEntity();
        companyTeamEntity.setId(UUIDUtils.getUUID());
        BeanUtils.copyProperties(companyTeamAddVo, companyTeamEntity);
        PeopleEntity peopleEntity = peopleRepository.findByName(companyTeamAddVo.getName());
        if (peopleEntity == null) {
            peopleEntity = new PeopleEntity();
            peopleEntity.setId(UUIDUtils.getUUID());
            peopleEntity.setUpdateTime(new Date());
            peopleEntity.setCreateTime(new Date());
            peopleEntity.setName(companyTeamAddVo.getName());
            peopleRepository.save(peopleEntity);
        }
        companyTeamEntity.setPeopleId(peopleEntity.getId());
        companyTeamRepository.save(companyTeamEntity);

    }

    @Override
    public void del(DelBody id) {
        companyTeamRepository.delete(id.getId());
    }


}
